﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Extensil.Common.Progress
{
    public partial class ProgressUpdate :Form
    {
        public string TaskLogFormat
        {
            get;
            set;
        }
        string DateFormat
        {
            get;
            set;
        }
        public ProgressUpdate()
            : this(String.Empty)
        {
        }
        public ProgressUpdate(string format)
        {
            InitializeComponent();
            pbTask.Maximum = 100;
            pbTask.Minimum = 0;
            pbMain.Maximum = 100;
            pbMain.Minimum = 0;
            TaskLogFormat = format;
        }
        public void Add(ProgressStatus ps, int progressPercentage)
        {
            if(ps.Source == StatusSource.Main)
            {
                pbMain.Value = progressPercentage;
                lblTaskName.Text = ps.Message;
            }
            else
            {
                pbTask.Value = ps.PercentProgress;
                if(rtbStatus.Text != "")
                    rtbStatus.AppendText("\r\n");
                rtbStatus.AppendText(FormatLog(ps.Message));
                rtbStatus.ScrollToCaret();
            }
        }
        string FormatLog(string message)
        {
            if(String.IsNullOrEmpty(TaskLogFormat))
                return message;
            string fmessage = TaskLogFormat.Replace("@DateTime", DateTime.Now.ToString(DateFormat));
            fmessage = fmessage.Replace("@LogMessage", message);
            return fmessage;
        }
    }
}
